﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ProgramMain.Google
{
    public class Coordinate : IComparable, ICloneable
    {
        public static readonly Coordinate Empty = new Coordinate();

        public double Longitude { get; set; }

        public double Latitude { get; set; }

        private Coordinate()
        {
            Latitude = 0;
            Longitude = 0;
        }

        public Coordinate(double pLongitude, double pLatitude)
        {
            Longitude = pLongitude;
            Latitude = pLatitude;
        }

        public Coordinate(decimal pLongitude, decimal pLatitude)
        {
            Longitude = (double)pLongitude;
            Latitude = (double)pLatitude;
        }

        #region ICloneable Members
        public object Clone()
        {
            return new Coordinate(Longitude, Latitude);
        }
        #endregion

        #region IComparable Members
        public int CompareTo(Object obj)
        {
            Coordinate coords = (Coordinate)obj;
            if (coords.Latitude < Latitude)
                return -1;
            else if (coords.Latitude > Latitude)
                return 1;
            else if (coords.Longitude < Longitude)
                return -1;
            else if (coords.Longitude > Longitude)
                return 1;
            else
                return 0;
        }
        #endregion


        public static Coordinate operator + (Coordinate coordinate, GoogleCoordinate Addon)
        {
            return new GoogleCoordinate(coordinate, Addon.Level) + Addon;
        }

        private GoogleCoordinate GetLeftTopGoogle(int ScreenWidth, int ScreenHeight, int level)
        {
            return new GoogleCoordinate(
                MapUtilities.GetGoogleX(this, level) - ((ScreenWidth + 1) / 2 - 1),
                MapUtilities.GetGoogleY(this, level) - ((ScreenHeight + 1) / 2 - 1),
                level);
        }

        private GoogleCoordinate GetRightBottomGoogle(int ScreenWidth, int ScreenHeight, int level)
        {
            return new GoogleCoordinate(
                MapUtilities.GetGoogleX(this, level) + ((ScreenWidth - 1) / 2 + 1),
                MapUtilities.GetGoogleY(this, level) + ((ScreenHeight - 1) / 2 + 1),
                level);
        }

        public GoogleRectangle GoogleScreenViewFromCenter(int ScreenWidth, int ScreenHeight, int level)
        {
            return new GoogleRectangle(GetLeftTopGoogle(ScreenWidth, ScreenHeight, level), GetRightBottomGoogle(ScreenWidth, ScreenHeight, level));
        }

        public Point GetScreenPoint(GoogleRectangle googleScreenView)
        {
            return new GoogleCoordinate(this, googleScreenView.Level).GetScreenPoint(googleScreenView);
        }

        public static Coordinate CoordinateFromScreen(GoogleRectangle googleScreenView, Point point)
        {
            return googleScreenView.LeftTop + new GoogleCoordinate(point.X, point.Y, googleScreenView.Level);
        }

        public GoogleBlock GetGoogleBlock(int level)
        {
            return new GoogleCoordinate(this, level);
        }

        public double Distance(Coordinate coordinate)
        {
            return EarthUtilities.GetLength(this, coordinate);
        }
    }
}
